// Copyright 2010 Google Inc. All Rights Reserved.

package com.google.zahra.test;

import java.io.BufferedReader;
import java.io.FileReader;

import redstone.xmlrpc.XmlRpcClient;

/**
 * @author arthurkok@google.com (Your Name Here)
 *
 */
public class RpcTester {

  public RpcTester(int numOfTests, int speedOfTests, String pythonFileLoc) throws Exception {
    
    XmlRpcClient client = new XmlRpcClient("http://75.101.162.43:8080/addTest", false);    
    
    String python = "";
    BufferedReader in = new BufferedReader(new FileReader(pythonFileLoc));  
    String str;
    
    while  ((str = in.readLine()) != null) {
      python += str + "\n";
    }
       
    int i = 0;
    
    // Loop for numOfTest, or infinity if numOfTest is zero (0)
    while(i < numOfTests || numOfTests == 0) {
      
      try {
        System.out.println("Sending test #" + i);
      client.invoke( "TestManager.addTestCase", 
          new Object[] {python.getBytes(), "RPC TEST NUMBER " + i++, 7, 3} );
      } catch (Exception e) {
        e.printStackTrace();
      }
      
      try {
        Thread.sleep(speedOfTests);
      } catch (InterruptedException e) {
        Thread.interrupted();
        e.printStackTrace();
      }      
    }
  }
  
  /**
   * @param args
   */
  public static void main(String[] args) {
    
    // Get the arguments
    int numOfTests = Integer.parseInt(args[0]);
    int speedOfTests = Integer.parseInt(args[1]);
    String pythonFileLoc = args[2];
    
    if (speedOfTests == 0) {
      speedOfTests = 1000;
    }
    
    try {
      RpcTester systemTester = new RpcTester(numOfTests, speedOfTests, pythonFileLoc);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

}
